package com.ns.scs.comments.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.comments.bo.CommentsBO;
import com.ns.scs.comments.bo.CommentsPageBO;
import com.ns.scs.comments.dto.CommentsPageDto;
import com.ns.scs.comments.entry.CommentsEntry;
import com.ns.scs.comments.service.ICommentsService;
import com.ns.scs.comments.vo.CommentsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 资源评论相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/comments")
@RequiredArgsConstructor
@CrossOrigin
public class CommentsController {

    private final ICommentsService service;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "评论模块", name = "增")
    public ResultOk add(@RequestBody CommentsBO bo) {
        CommentsEntry entry = new CommentsEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "评论模块", name = "改")
    public ResultOk update(@RequestBody CommentsBO bo) {
        CommentsEntry entry = new CommentsEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.updateById(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "评论模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "评论模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "评论模块", name = "详情")
    public ResultOk get(Long id) {
        return ResultOk.getSuccessData(service.getById(id));
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "评论模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody CommentsPageBO bo) {
        CommentsPageDto dto = new CommentsPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<CommentsVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("comment")
    @ReqLog(module = "评论模块", name = "评论")
    public ResultOk comment(@RequestBody CommentsBO bo) {
        CommentsEntry entry = new CommentsEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setCommentator(StpUtil.getLoginUserId());
        return ResultOk.getSuccessData(service.comment(entry));
    }

    /**
     * 逻辑删自己评论
     *
     * @param id 参数
     */
    @GetMapping("delOwner")
    @ReqLog(module = "评论模块", name = "逻辑删")
    ResultOk delOwner(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return ResultOk.getError("请选择需要删除的评论！");
        }
        CommentsEntry comment = service.getById(id);
        // 判空
        if (ObjectUtils.isEmpty(comment)) {
            return ResultOk.getError("未获取到评论信息！");
        }
        if (ObjectUtils.nullSafeEquals(comment.getCommentator(), StpUtil.getLoginUserId())) {
            return ResultOk.getByBoolean(service.removeById(id));
        } else {
            return ResultOk.getError("只能删除自己的评论！");
        }
    }

    /**
     * 查询资源相关评论
     *
     * @param resourceId 参数
     */
    @GetMapping("getByResourceId")
    @ReqLog(module = "评论模块", name = "详情", authFlag = false)
    public ResultOk getBySourceId(Long resourceId) {
        return ResultOk.getSuccessData(service.getByResourceId(resourceId));
    }

    /**
     * 评论点赞
     *
     * @param id 参数
     */
    @GetMapping("thumbsUp")
    @ReqLog(module = "评论模块", name = "详情")
    public ResultOk thumbsUp(Long id) {
        return ResultOk.getSuccessData(service.thumbsUp(id, StpUtil.getLoginUserId()));
    }


    /**
     * 审核
     *
     * @param id     id
     * @param status 状态
     */
    @GetMapping("audit")
    @ReqLog(module = "评论模块", name = "审核")
    public ResultOk audit(Long id, Integer status) {
        return ResultOk.getByBoolean(service.audit(id, status));
    }
}
